<template>
  <el-dialog
    :title="dataRow.title"
    :visible.sync="dialogVisible"
    width="40%"
    :close-on-click-modal="false"
    :before-close="handleClose">
    <el-form :model="ruleForm" ref="ruleForm" label-width="120px" class="demo-ruleForm">
     <el-row>
      <el-col :span="24">
        <el-form-item v-for="item,index in formList" :key="index" :label="!item.isPk ? item.label : ''" :prop="item.prop">
          <div v-if="item.isRegion">
            <el-tree v-if="dataRow.title === '查看数据资源' ? false :true" :data="treeData" ref="treeForm" :props="defaultProps" @check="(node, list)=>handleCheck(node, list,item)" show-checkbox node-key="label" :check-strictly="true" :disabled="dataRow.title === '查看数据资源'"></el-tree>
            <el-input v-model="ruleForm[item.prop]" size="small" v-if="dataRow.title === '查看数据资源' ? true :false" :disabled="dataRow.title === '查看数据资源'" ></el-input>
          </div>
          <div v-if="item.isDict">
            <el-select v-model="ruleForm[item.prop]" placeholder="请选择" size="small" style="width: 100%;" :disabled="dataRow.title === '查看数据资源'">
              <el-option
                v-for="item in item.list"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              ></el-option>
            </el-select>
          </div>
          <div v-if="!item.isDict &&  !item.isRegion ">
            <el-input v-if="!item.isPk" v-model="ruleForm[item.prop]" size="small" :disabled="dataRow.title === '查看数据资源'"></el-input>
          </div>
        </el-form-item>
      </el-col>
     </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose" size="small">取 消</el-button>
      <el-button type="primary" @click="save" size="small">确 定</el-button>
    </span>
  </el-dialog>

</template>

<script>
import * as API from '../../api/resourceManagement/index'
export default {
  props: {
    dataRow: {
      type: Object,
      default: () => {}
    },
    nodeId: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      regionList: [],
      formList: [],
      dialogVisible: this.dataRow.dataBaseVisible,
      ruleForm: {
        HazardName: null,
        administrative: null,
        lonlattude: null,
        dangerLevel: null,
        stability: null,
        households: null,
        population: null,
        monitoringPersonnel: null,
        monitoringTelephone: null,
        monitoringMethod: null,
        protection: null,
        alarm: null,
        disaster: null,
        evacuation: null
      },
      treeData: [],
      rules: {
        HazardName: [
          {
            required: true, message: '请输入隐患点名称', trigger: 'blur'
          }
        ],
        administrative: [
          {
            required: true, message: '请选择行政区划', trigger: 'change'
          }
        ],
        lonlattude: [
          {
            required: true, message: '请输入经纬度用,隔开', trigger: 'blur'
          }
        ],
        dangerLevel: [
          {
            required: true, message: '请选择险情等级', trigger: 'change'
          }
        ],
        stability: [
          {
            required: true, message: '请选择稳定性', trigger: 'change'
          }
        ],
        households: [
          {
            required: true, message: '请输入威胁户数', trigger: 'blur'
          }
        ],
        population: [
          {
            required: true, message: '请输入威胁人数', trigger: 'blur'
          }
        ],
        monitoringPersonnel: [
          {
            required: true, message: '请输入监测人员', trigger: 'blur'
          }
        ],
        monitoringTelephone: [
          {
            required: true, message: '请输入监测电话', trigger: 'blur'
          }
        ],
        monitoringMethod: [
          {
            required: true, message: '请输入监测方法', trigger: 'blur'
          }
        ],
        protection: [
          {
            required: true, message: '请输入防灾措施', trigger: 'blur'
          }
        ],
        alarm: [
          {
            required: true, message: '请输入报警方式', trigger: 'blur'
          }
        ],
        disaster: [
          {
            required: true, message: '请输入避灾地点', trigger: 'blur'
          }
        ],
        evacuation: [
          {
            required: true, message: '请输入人员撤离路线', trigger: 'blur'
          }
        ]
      },

      defaultProps: {
        children: 'children',
        label: 'label'
      }
    }
  },
  mounted() {
    this.getManagementGetTable()
    if (this.dataRow.title === '编辑数据资源' || this.dataRow.title === '查看数据资源') {
      this.getManagementGetRecord()
    }
  },
  methods: {
    async getManagementGetRecord() {
      await API.managementGetRecord(this.nodeId, this.dataRow.pkName).then(res => {
        if (res.code === 200) {
          this.ruleForm = res.data
          console.log(this.formList, this.dataRow.row, 'this.dataRow')
          this.formList.forEach(item => {
            if (item.isRegion) {
              for (const key in this.dataRow.row) {
                if (key === item.columnName) {
                  this.$refs.treeForm[0].setCheckedKeys([res.data[key]])
                }
              }
            }
          })
        }
      })
    },
    getManagementGetTable() {
      API.managementGetTable({ catalogId: this.nodeId }).then(res => {
        if (res.code === 200) {
          res.data.columns.forEach(item => {
            if (item.isDict && item.dictType) {
              API.dictDataTtpe(item.dictType).then(res => {
                if (res.code === 200) {
                  item.list = res.data
                  this.formList.push({
                    ...item,
                    label: item.columnAlias,
                    prop: item.columnName,
                    isRegion: item.isRegion,
                    isDict: item.isDict,
                    dictType: item.dictType,
                    list: item.list
                  })
                }
              })
            } else {
              this.formList.push({
                ...item,
                label: item.columnAlias,
                prop: item.columnName,
                isRegion: item.isRegion,
                isDict: item.isDict
              })
            }
          })

          this.getAreaTreeselect()
        }
      })
    },

    getAreaTreeselect() {
      API.areaTreeselect().then(res => {
        if (res.code === 200) {
          this.treeData = res.data
        }
      })
    },
    handleCheck(node, list, item) {
      if (list.checkedKeys.length === 2) {
        this.$refs.treeForm[0].setCheckedKeys([node.label])
      }

      this.$set(this.ruleForm, item.columnName, (this.$refs.treeForm[0].getCheckedKeys())[0])
    },
    handleClose() {
      this.$refs.ruleForm.resetFields()
      this.$emit('handleClose')
    },
    save() {
      const arrayList = []
      const allArrayList = []
      this.formList.forEach(item => {
        for (const key in this.ruleForm) {
          if (item.columnName === key) {
            arrayList.push({
              'columnName': key,
              'columnType': item.columnType,
              'columnValue': this.ruleForm[key],
              'dataType': item.dataType,
              'geomType': item.geomType
            })
          }
        }
      })
      allArrayList.push(arrayList)

      if (this.dataRow.title === '新增数据资源') {
        const data = {
          'catalogId': this.nodeId,
          'datas': allArrayList
        }
        API.managementAddRecords(data).then(res => {
          if (res.code === 200) {
            this.handleClose()
          }
        })
      } else if (this.dataRow.title === '编辑数据资源') {
        allArrayList[0].forEach(item => {
          item.id = this.dataRow.pkName
        })
        const name = this.dataRow.pkName
        const obj = {}
        this.$set(obj, name, allArrayList[0])
        const data = {
          'catalogId': this.nodeId,
          'datas': obj
        }
        API.managementUpdateRecords(data).then(res => {
          if (res.code === 200) {
            this.handleClose()
          }
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
:deep(.el-select-dropdown){
  background: rgba(0,28,55,0.85) !important
}
:deep(.el-select-dropdown__item.hover){
   background-color: rgba(0,28,55,0.85) !important
}
</style>
